package v1

import (
	"blog/internal/common"
	"blog/internal/entry"
	"blog/internal/pkg/response"
	"blog/internal/services"
	"fmt"
	"github.com/gin-gonic/gin"
)

type AuthController struct {
	UserService services.IUserService
}

func NewAuthController(userService services.IUserService) *AuthController {
	return &AuthController{
		UserService: userService,
	}
}

// Login 登录
func (auth *AuthController) Login(ctx *gin.Context) {

	var request entry.LoginRequest
	if err := ctx.ShouldBind(&request); err != nil {
		response.Fail(ctx, common.COMMON_ERROR, err.Error())
		return
	}

	token, err := auth.UserService.Login(ctx, request.Username, request.Password)

	if err != nil {
		response.Fail(ctx, common.COMMON_ERROR, err.Error())
		return
	}
	response.Data(ctx, token)
}

// Logout 退出账号
func (auth *AuthController) Logout(ctx *gin.Context) {
	uid := ctx.GetUint64("userid")
	if uid < 1 {
		response.Fail(ctx, common.COMMON_ERROR, "退出账号失败")
		return
	}

	err := auth.UserService.Logout(ctx, uid)
	if err != nil {
		response.Fail(ctx, common.COMMON_ERROR, "退出账号失败")
		return
	}
	response.Success(ctx, "退出成功")
}

func (auth *AuthController) Info(ctx *gin.Context) {
	uid := ctx.GetUint64("userid")
	if uid < 1 {
		response.Fail(ctx, common.COMMON_ERROR, "退出账号失败")
		return
	}
	profile, err := auth.UserService.Info(ctx, uid)
	fmt.Println(profile, err)
}
